﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApplication1.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WindowsFormsApplication1.Utils;

namespace WindowsFormsApplication1.ResourceManager
{

    public class AreaConfiguration
    {
        private static AreaConfiguration _instance;
        private static readonly object padlock = new object();

        public List<AreaResourceInfo> AreaInfos { get; set; }

        private AreaConfiguration()
        {}


        public static AreaConfiguration Instance
        {
            get
            {
                lock (padlock)
                {
                    if (_instance == null)
                    {
                        _instance = BuidAreaConfiguration();
                        return _instance;
                    }
                    else
                    {
                        return _instance;
                    }
                }
            }
        }


        private static AreaConfiguration BuidAreaConfiguration()
        {
            //var jsonStr = FileHelper.ReadJsonFile();
            var jsonStr = HttpHelper.Get("https://gitee.com/luhui123/qqhuaxiajuesechaxunqufupeizhi/raw/master/area.json", null, null);
            JArray jAraay = (JArray)JsonConvert.DeserializeObject(jsonStr);
            List<AreaResourceInfo> areInfos = new List<AreaResourceInfo>();
            if(jAraay!=null && jAraay.Count > 0)
            {
                for (int i = 0; i < jAraay.Count; i++)
                {
                    var areaResourceInfo = jAraay[i].ToObject<AreaResourceInfo>();
                    areInfos.Add(areaResourceInfo);
                }
            }
            AreaConfiguration areaConfiguration = new AreaConfiguration();
            areaConfiguration.AreaInfos = areInfos;
            return areaConfiguration;
        }
    }
}
